home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 May / Macworld (1999-05).dmg / Updaters / SpriteWorld 2.2.1 Update / Source Updates / SpriteWorldUtils.c < prev   
Text File  |  1999-02-25  |  20KB  |  811 lines

  1. ///--------------------------------------------------------------------------------------
  2. //    SpriteWorldUtils.c
  3. //
  4. //    Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
  5. //
  6. //    Description:    some utilities for creating worlds of sprites
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef __SWCOMMON__
  11. #include "SWCommonHeaders.h"
  12. #endif
  13.  
  14. #ifndef __QUICKDRAW__
  15. #include <QuickDraw.h>
  16. #endif
  17.  
  18. #ifndef __QDOFFSCREEN__
  19. #include <QDOffscreen.h>
  20. #endif
  21.  
  22. #ifndef __WINDOWS__
  23. #include <Windows.h>
  24. #endif
  25.  
  26. #ifndef __MEMORY__
  27. #include <Memory.h>
  28. #endif
  29.  
  30. #ifndef __GESTALT__
  31. #include <Gestalt.h>
  32. #endif
  33.  
  34. #ifndef __TOOLUTILS__
  35. #include <ToolUtils.h>
  36. #endif
  37.  
  38. #ifndef __RESOURCES__
  39. #include <Resources.h>
  40. #endif
  41.  
  42. #ifndef __ERRORS__
  43. #include <Errors.h>
  44. #endif
  45.  
  46. #ifndef __SPRITEWORLD__
  47. #include "SpriteWorld.h"
  48. #endif
  49.  
  50. #ifndef __SPRITELAYER__
  51. #include "SpriteLayer.h"
  52. #endif
  53.  
  54. #ifndef __SPRITE__
  55. #include "Sprite.h"
  56. #endif
  57.  
  58. #ifndef __SPRITEFRAME__
  59. #include "SpriteFrame.h"
  60. #endif
  61.  
  62. #ifndef __SPRITEWORLDUTILS__
  63. #include "SpriteWorldUtils.h"
  64. #endif
  65.  
  66.  
  67. static OSErr                gSWStickyError;
  68.  
  69.  
  70. ///--------------------------------------------------------------------------------------
  71. // SWCreateRegionFromCIconMask
  72. ///--------------------------------------------------------------------------------------
  73.  
  74. SW_FUNC OSErr SWCreateRegionFromCIconMask(
  75.     RgnHandle *maskRgn,
  76.     CIconHandle cIconH)
  77. {
  78.     OSErr err = noErr;
  79.     RgnHandle tempMaskRgn;
  80.     char saveState;
  81.     BitMap iconMask;
  82.  
  83.     *maskRgn = NULL;
  84.  
  85.     saveState = HGetState((Handle)cIconH);
  86.     HLock((Handle)cIconH);
  87.  
  88.     iconMask.rowBytes = (**cIconH).iconMask.rowBytes;
  89.     iconMask.bounds = (**cIconH).iconMask.bounds;
  90.     iconMask.baseAddr = (Ptr)(**cIconH).iconMaskData;
  91.  
  92.     tempMaskRgn = NewRgn();
  93.  
  94.     if (tempMaskRgn != NULL)
  95.     {
  96.         err = BitMapToRegion(tempMaskRgn, &iconMask);
  97.  
  98.         if (err == noErr)
  99.         {
  100.             *maskRgn = tempMaskRgn;
  101.         }
  102.         else
  103.         {
  104.             DisposeRgn(tempMaskRgn);
  105.         }
  106.     }
  107.     else
  108.     {
  109.         err = MemError();
  110.     }
  111.  
  112.     HSetState((Handle)cIconH, saveState);
  113.  
  114.     SWSetStickyIfError( err );
  115.     return err;
  116. }
  117.  
  118.  
  119.  
  120. ///--------------------------------------------------------------------------------------
  121. // SWCreateRegionFromPict
  122. ///--------------------------------------------------------------------------------------
  123.  
  124. SW_FUNC OSErr SWCreateRegionFromPict(
  125.     RgnHandle *pictRgnH,
  126.     PicHandle srcPictH)
  127. {
  128.     RgnHandle             tempRgnH;
  129.     GWorldPtr             saveGWorld, 
  130.                         offScrnGWld;
  131.     GDHandle            saveGDH;
  132.     Rect                frameRect;
  133.     ColorSearchUPP        blackenColSearchUPP;
  134.     OSErr                 err;
  135.     
  136.     GetGWorld(&saveGWorld, &saveGDH );
  137.     
  138.     *pictRgnH = NULL;
  139.     frameRect = (**srcPictH).picFrame;
  140.     OffsetRect(&frameRect, -frameRect.left, -frameRect.top);
  141.         
  142.     err = NewGWorld( &offScrnGWld, 1, &frameRect, nil, nil, 0 );
  143.     
  144.     if (err == noErr)
  145.     {    
  146.         blackenColSearchUPP = NewColorSearchProc( blackenColorSearch );
  147.         (void)LockPixels( GetGWorldPixMap(offScrnGWld) );
  148.         SetGWorld(offScrnGWld, nil);
  149.         AddSearch( blackenColSearchUPP );
  150.         EraseRect(&frameRect);
  151.         DrawPicture(srcPictH, &frameRect);
  152.         DelSearch( blackenColSearchUPP );
  153.         DisposeRoutineDescriptor( blackenColSearchUPP );
  154.         tempRgnH = NewRgn();
  155.  
  156.         if (tempRgnH != NULL && err == noErr)
  157.         {
  158.             err = BitMapToRegion(tempRgnH, (BitMap*)*GetGWorldPixMap( offScrnGWld ) );
  159.             
  160.             if (err == noErr)
  161.             {
  162.                 *pictRgnH = tempRgnH;
  163.             }
  164.             else
  165.             {
  166.                 DisposeRgn(tempRgnH);
  167.             }
  168.         }
  169.         else
  170.         {
  171.             if ( err == noErr )
  172.                 err = MemError();
  173.         }
  174.  
  175.         SetGWorld(saveGWorld, nil);
  176.         DisposeGWorld(offScrnGWld);
  177.     }
  178.  
  179.     SWSetStickyIfError( err );
  180.     return err;
  181. }
  182.  
  183. ///--------------------------------------------------------------------------------------
  184. // SWCreateRegionFromGWorldAndRect
  185. ///--------------------------------------------------------------------------------------
  186.  
  187. SW_FUNC OSErr SWCreateRegionFromGWorldAndRect(
  188.     RgnHandle *maskRgn,
  189.     GWorldPtr maskGWorld,
  190.     Rect* frameRect)
  191. {
  192.     OSErr             err = noErr;
  193.     GDHandle        saveGDH;
  194.     GWorldPtr         saveGWorld, 
  195.                     tempMaskGWorld;
  196.     Rect            tempMaskRect;
  197.     
  198.     
  199.     *maskRgn = NULL;
  200.     
  201.     GetGWorld(&saveGWorld, &saveGDH);
  202.  
  203.     tempMaskRect = *frameRect;
  204.     OffsetRect(&tempMaskRect, -tempMaskRect.left, -tempMaskRect.top);
  205.     
  206.     err = SWCreateRegionFromGWorldAndRectStart( &tempMaskGWorld, tempMaskRect.right, 
  207.             tempMaskRect.bottom );
  208.     
  209.     if (err == noErr)
  210.     {
  211.         err = SWCreateRegionFromGWorldAndRectPartial( maskRgn, maskGWorld,
  212.                 tempMaskGWorld, frameRect );
  213.         SWCreateRegionFromGWorldAndRectFinish( tempMaskGWorld );
  214.     }
  215.  
  216.     SWSetStickyIfError( err );
  217.     return err;
  218. }
  219.  
  220.  
  221. ///--------------------------------------------------------------------------------------
  222. // SWCreateRegionFromGWorldAndRectStart
  223. ///--------------------------------------------------------------------------------------
  224.  
  225. SW_FUNC OSErr SWCreateRegionFromGWorldAndRectStart(
  226.     GWorldPtr *tempMaskGWorld,
  227.     short maxWidth,
  228.     short maxHeight)
  229. {
  230.     OSErr         err = noErr;
  231.     Rect         tempMaskRect;
  232.     
  233.     
  234.     SetRect( &tempMaskRect, 0, 0, maxWidth, maxHeight );
  235.     err = NewGWorld( tempMaskGWorld, 1, &tempMaskRect, nil, nil, 0 );
  236.     if (err == noErr)
  237.     {
  238.         (void)LockPixels( GetGWorldPixMap( *tempMaskGWorld ) );
  239.     }
  240.     
  241.     SWSetStickyIfError( err );
  242.     return err;
  243. }
  244.  
  245. ///--------------------------------------------------------------------------------------
  246. // SWCreateRegionFromGWorldAndRectFinish
  247. ///--------------------------------------------------------------------------------------
  248.  
  249. SW_FUNC void SWCreateRegionFromGWorldAndRectFinish(
  250.     GWorldPtr tempMaskGWorld)
  251. {    
  252.     if ( tempMaskGWorld != NULL )
  253.     {
  254.         DisposeGWorld(tempMaskGWorld);
  255.     }
  256. }
  257.  
  258. ///--------------------------------------------------------------------------------------
  259. // SWCreateRegionFromGWorldAndRectPartial
  260. ///--------------------------------------------------------------------------------------
  261.  
  262. SW_FUNC OSErr SWCreateRegionFromGWorldAndRectPartial(
  263.     RgnHandle *maskRgn,
  264.     GWorldPtr maskGWorld,
  265.     GWorldPtr tempMaskGWorld,
  266.     Rect* frameRect)
  267. {
  268.     OSErr             err = noErr;
  269.     GDHandle        saveGDH;
  270.     GWorldPtr         saveGWorld;
  271.     Rect            tempMaskRect;
  272.     RgnHandle         tempRgnH;
  273.     
  274.     
  275.     *maskRgn = NULL;
  276.     
  277.     GetGWorld(&saveGWorld, &saveGDH);
  278.  
  279.     tempMaskRect = *frameRect;
  280.     OffsetRect(&tempMaskRect, -tempMaskRect.left, -tempMaskRect.top);
  281.     
  282.     (void)LockPixels( GetGWorldPixMap(maskGWorld) );
  283.     SetGWorld(maskGWorld, nil);
  284.     SetGWorld(tempMaskGWorld, nil);
  285.         
  286.     CopyBits( (BitMap*)*GetGWorldPixMap( maskGWorld ), 
  287.         (BitMap*)*GetGWorldPixMap( tempMaskGWorld ), 
  288.         frameRect, 
  289.         &tempMaskRect,
  290.         srcCopy, 
  291.         nil );
  292.  
  293.     tempRgnH = NewRgn();
  294.     if (tempRgnH != NULL)
  295.     {
  296.         err = BitMapToRegion(tempRgnH, (BitMap*)*GetGWorldPixMap( tempMaskGWorld ) );
  297.  
  298.         UnlockPixels( GetGWorldPixMap(maskGWorld) );
  299.         
  300.         if (err == noErr)
  301.         {
  302.             *maskRgn = tempRgnH;
  303.         }
  304.         else
  305.         {
  306.             DisposeRgn(tempRgnH);
  307.         }
  308.     }
  309.     else
  310.     {
  311.         err = MemError();
  312.     }
  313.     SetGWorld(saveGWorld, nil);
  314.     
  315.     SWSetStickyIfError( err );
  316.     return err;
  317. }
  318.  
  319.  
  320. ///--------------------------------------------------------------------------------------
  321. // SWCreateGWorldFromPictResource
  322. ///--------------------------------------------------------------------------------------
  323.  
  324. SW_FUNC OSErr SWCreateGWorldFromPictResource(
  325.     SpriteWorldPtr destSpriteWorld,
  326.     GWorldPtr *pictGWorldP,
  327.     short pictResID)
  328. {
  329.     OSErr             err = noErr;
  330.     PicHandle         newPictH;
  331.     
  332.     
  333.     newPictH = GetPicture(pictResID);
  334.  
  335.     if (newPictH != NULL)
  336.     {
  337.         err = SWCreateGWorldFromPict(destSpriteWorld, pictGWorldP, newPictH);
  338.  
  339.         ReleaseResource((Handle)newPictH);
  340.     }
  341.     else
  342.     {
  343.         err = ResError();
  344.         if (err == noErr)
  345.         {
  346.             err = resNotFound;
  347.         }
  348.     }
  349.  
  350.     SWSetStickyIfError( err );
  351.     return err;
  352. }
  353.  
  354.  
  355. ///--------------------------------------------------------------------------------------
  356. // SWCreateGWorldFromPict
  357. //    creates a offScreen GWorld and draws the specified pict into it
  358. ///--------------------------------------------------------------------------------------
  359.  
  360. SW_FUNC OSErr SWCreateGWorldFromPict(
  361.     SpriteWorldPtr destSpriteWorld,
  362.     GWorldPtr *pictGWorld,
  363.     PicHandle pictH)
  364. {
  365.     OSErr            err;
  366.     GWorldPtr         saveGWorld;
  367.     GDHandle         saveGDevice;
  368.     GWorldPtr         tempGWorld;
  369.     Rect             pictRect;
  370.     short            depth;
  371.     GDHandle        theGDH;
  372.  
  373.  
  374.     *pictGWorld = NULL;
  375.         
  376.     GetGWorld(&saveGWorld, &saveGDevice);
  377.     
  378.     depth = destSpriteWorld->pixelDepth;
  379.     theGDH = destSpriteWorld->mainSWGDH;
  380.  
  381.     pictRect = (**pictH).picFrame;
  382.     OffsetRect(&pictRect, -pictRect.left, -pictRect.top);
  383.  
  384.     if ( (**((**theGDH).gdPMap)).pixelSize == depth )
  385.         err = NewGWorld( &tempGWorld, depth, &pictRect, nil, theGDH, noNewDevice );
  386.     else
  387.         err = NewGWorld( &tempGWorld, depth, &pictRect, nil, nil, 0 );
  388.     
  389.     if (err == noErr)
  390.     {    
  391.         *pictGWorld = tempGWorld;
  392.  
  393.         SetGWorld(tempGWorld, nil);
  394.  
  395.         (void)LockPixels( GetGWorldPixMap(tempGWorld) );
  396.         EraseRect(&pictRect);
  397.         DrawPicture(pictH, &pictRect);
  398.         UnlockPixels( GetGWorldPixMap(tempGWorld) );
  399.     }
  400.  
  401.     SetGWorld(saveGWorld, saveGDevice);
  402.     
  403.     SWSetStickyIfError( err );
  404.     return err;
  405. }
  406.  
  407.  
  408. ///--------------------------------------------------------------------------------------
  409. // SWCreateGWorldFromCIconMask
  410. ///--------------------------------------------------------------------------------------
  411.  
  412. SW_FUNC OSErr SWCreateGWorldFromCIconMask(
  413.     SpriteWorldPtr destSpriteWorld,
  414.     GWorldPtr *maskGWorldP,
  415.     CIconHandle cIconH)
  416. {
  417.     OSErr             err;
  418.     GWorldPtr         saveGWorld;
  419.     GDHandle         saveGDevice;
  420.     char             saveState;
  421.     BitMap             maskBitMap;
  422.     GWorldPtr         tempGWorldP;
  423.     short            depth;
  424.     GDHandle        theGDH;
  425.  
  426.  
  427.     GetGWorld(&saveGWorld, &saveGDevice);
  428.  
  429.     depth = destSpriteWorld->pixelDepth;
  430.     theGDH = destSpriteWorld->mainSWGDH;
  431.     
  432.     saveState = HGetState((Handle)cIconH);
  433.     HLock((Handle)cIconH);
  434.  
  435.     maskBitMap.rowBytes = (**cIconH).iconMask.rowBytes;
  436.     maskBitMap.bounds = (**cIconH).iconMask.bounds;
  437.     maskBitMap.baseAddr = (Ptr)(**cIconH).iconMaskData;
  438.     
  439.     if ( (**((**theGDH).gdPMap)).pixelSize == depth )
  440.         err = NewGWorld( &tempGWorldP, depth, &maskBitMap.bounds, nil, theGDH, noNewDevice );
  441.     else
  442.         err = NewGWorld( &tempGWorldP, depth, &maskBitMap.bounds, nil, nil, 0 );
  443.                 
  444.     if (err == noErr)
  445.     {    
  446.         SetGWorld(tempGWorldP, NULL);
  447.         
  448.         (void)LockPixels( GetGWorldPixMap(tempGWorldP) );
  449.         CopyBits(&maskBitMap, 
  450.             (BitMap*)*GetGWorldPixMap( tempGWorldP ),
  451.             &maskBitMap.bounds, 
  452.             &maskBitMap.bounds,
  453.             srcCopy, 
  454.             nil);        
  455.         UnlockPixels( GetGWorldPixMap(tempGWorldP) );
  456.         
  457.         *maskGWorldP = tempGWorldP;
  458.     }
  459.  
  460.     SetGWorld(saveGWorld, saveGDevice);
  461.  
  462.     HSetState((Handle)cIconH, saveState);
  463.  
  464.     SWSetStickyIfError( err );
  465.     return err;
  466. }
  467.  
  468.  
  469. ///--------------------------------------------------------------------------------------
  470. //    SWSetTransparentColor - allows you to set the background, or unmasked color
  471. //    of a sprite to a color other than white. Important: the sprite's background color
  472. //    MUST match the color you use with SWTransparentColor, or the Sprite won't load correctly.
  473. ///--------------------------------------------------------------------------------------
  474.  
  475. static RGBColor    SWTransparentColor = { 0xFFFF, 0xFFFF, 0xFFFF };        // white
  476.  
  477. SW_FUNC void SWSetTransparentColor(const RGBColor *RGB)
  478. {
  479.     SWTransparentColor = *RGB;
  480. }
  481.  
  482.  
  483. ///--------------------------------------------------------------------------------------
  484. //    SWBlackenGWorld
  485. ///--------------------------------------------------------------------------------------
  486.  
  487. SW_FUNC OSErr SWBlackenGWorld( GWorldPtr oldGWorld )
  488. {
  489.     GWorldPtr            saveGWorld, newGWld;
  490.     GDHandle            saveGDH;
  491.     Rect                gwldRect;
  492.     GWorldFlags            pixelState;
  493.     ColorSearchUPP        blackenColSearchUPP;
  494.     OSErr                err;
  495.         
  496.     GetGWorld( &saveGWorld, &saveGDH );
  497.     
  498.     gwldRect = oldGWorld->portRect;
  499.     pixelState = GetPixelsState( oldGWorld->portPixMap );
  500.     
  501.     err = NewGWorld(&newGWld, 1, &gwldRect, nil, nil, 0 );
  502.     
  503.     if ( err == noErr )
  504.     {
  505.         blackenColSearchUPP = NewColorSearchProc( blackenColorSearch );
  506.         (void)LockPixels( GetGWorldPixMap(newGWld) );
  507.         (void)LockPixels( GetGWorldPixMap(oldGWorld) );
  508.         SetGWorld( oldGWorld, nil ); ForeColor(blackColor); BackColor(whiteColor);
  509.         SetGWorld( newGWld, nil );
  510.         AddSearch( blackenColSearchUPP );
  511.         CopyBits ( (BitMap*)*GetGWorldPixMap( oldGWorld ),
  512.             (BitMap*)*GetGWorldPixMap( newGWld ),
  513.             &gwldRect, 
  514.             &gwldRect, 
  515.             srcCopy, 
  516.             nil);
  517.         DelSearch( blackenColSearchUPP );
  518.         DisposeRoutineDescriptor( blackenColSearchUPP );
  519.         
  520.         SetGWorld( oldGWorld, nil );
  521.         CopyBits ( (BitMap*)*GetGWorldPixMap( newGWld ),
  522.             (BitMap*)*GetGWorldPixMap( oldGWorld ),
  523.             &gwldRect, 
  524.             &gwldRect, 
  525.             srcCopy, 
  526.             nil);
  527.         
  528.         SetPixelsState( oldGWorld->portPixMap, pixelState );
  529.         DisposeGWorld( newGWld );
  530.     }
  531.     
  532.     SetGWorld( saveGWorld, saveGDH );
  533.     
  534.     SWSetStickyIfError( err );
  535.     return err;
  536. }
  537.  
  538.  
  539. ///--------------------------------------------------------------------------------------
  540. //    SWWhitenGWorld - this function is called whenever SWBlackenGWorld is called to change
  541. //    all pixels in the Frame's image matching SWTransparentColor to white. This function,
  542. //    along with SWSetTransparentColor, allows you to load self-masking sprites without
  543. //    requiring the "transparent" color to be white. (With an additional delay, that is.)
  544. ///--------------------------------------------------------------------------------------
  545.  
  546. static unsigned short    SWTransparentValue;
  547.  
  548. SW_FUNC OSErr SWWhitenGWorld( GWorldPtr oldGWorld )
  549. {
  550.     GWorldPtr            saveGWorld, newGWld;
  551.     GDHandle            saveGDH;
  552.     Rect                gwldRect;
  553.     GWorldFlags            pixelState;
  554.     short                pixelSize;
  555.     ColorSearchUPP        whitenColSearchUPP;
  556.     OSErr                err;
  557.     
  558.     GetGWorld( &saveGWorld, &saveGDH );
  559.     
  560.     pixelSize = (**(oldGWorld->portPixMap)).pixelSize;
  561.     
  562.         // Compute index 0 for the current depth
  563.     if ( pixelSize <= 8 )
  564.         SWTransparentValue = 0xFFFF;        // white, becomes index 0
  565.     else
  566.         SWTransparentValue = 0x0000;        // black, is RGB 0
  567.     
  568.         // If SWTransparentColor is already index 0, then this function is not needed
  569.     if (SWTransparentColor.red == SWTransparentValue    &&
  570.         SWTransparentColor.green == SWTransparentValue    &&
  571.         SWTransparentColor.blue == SWTransparentValue    )
  572.     {
  573.         return noErr;
  574.     }
  575.     
  576.     gwldRect = oldGWorld->portRect;
  577.     err = NewGWorld(&newGWld, pixelSize, &gwldRect, nil, nil, 0 );
  578.     
  579.     if ( err == noErr )
  580.     {
  581.         pixelState = GetPixelsState( oldGWorld->portPixMap );
  582.         whitenColSearchUPP = NewColorSearchProc( whitenColorSearch );
  583.         (void)LockPixels( GetGWorldPixMap(newGWld) );
  584.         (void)LockPixels( GetGWorldPixMap(oldGWorld) );
  585.         SetGWorld( oldGWorld, nil ); ForeColor(blackColor); BackColor(whiteColor);
  586.         SetGWorld( newGWld, nil );
  587.         AddSearch( whitenColSearchUPP );
  588.         CopyBits ( (BitMap*)*GetGWorldPixMap( oldGWorld ),
  589.             (BitMap*)*GetGWorldPixMap( newGWld ),
  590.             &gwldRect, 
  591.             &gwldRect, 
  592.             srcCopy, 
  593.             nil);
  594.         DelSearch( whitenColSearchUPP );
  595.         DisposeRoutineDescriptor( whitenColSearchUPP );
  596.         
  597.         SetGWorld( oldGWorld, nil );
  598.         CopyBits ( (BitMap*)*GetGWorldPixMap( newGWld ),
  599.             (BitMap*)*GetGWorldPixMap( oldGWorld ),
  600.             &gwldRect, 
  601.             &gwldRect, 
  602.             srcCopy, 
  603.             nil);
  604.         
  605.         SetPixelsState( oldGWorld->portPixMap, pixelState );
  606.         DisposeGWorld( newGWld );
  607.     }
  608.     
  609.     SetGWorld( saveGWorld, saveGDH );
  610.     
  611.     SWSetStickyIfError( err );
  612.     return err;
  613. }
  614.  
  615.  
  616. ///--------------------------------------------------------------------------------------
  617. //    blackenColorSearch
  618. ///--------------------------------------------------------------------------------------
  619.  
  620. pascal Boolean blackenColorSearch( RGBColor *RGB, long* position )
  621. {
  622.     #pragma unused(position)
  623.     
  624.     if (    RGB->red != SWTransparentColor.red        ||
  625.             RGB->green != SWTransparentColor.green    ||
  626.             RGB->blue != SWTransparentColor.blue    )
  627.     {
  628.         RGB->red = RGB->green = RGB->blue = 0x0000;        // masked (black)
  629.     }
  630.     else
  631.     {
  632.         RGB->red = RGB->green = RGB->blue = 0xFFFF;        // transparent (white, index 0)
  633.     }
  634.     return ( false );
  635. }
  636.  
  637.  
  638. ///--------------------------------------------------------------------------------------
  639. //    whitenColorSearch
  640. ///--------------------------------------------------------------------------------------
  641.  
  642. pascal Boolean whitenColorSearch( RGBColor *RGB, long* position )
  643. {
  644.     #pragma unused(position)
  645.     
  646.         // Convert pixels matching SWTransparentColor to 0, leaving others untouched
  647.     if (    RGB->red == SWTransparentColor.red        &&
  648.             RGB->green == SWTransparentColor.green    &&
  649.             RGB->blue == SWTransparentColor.blue    )
  650.     {
  651.         RGB->red = RGB->green = RGB->blue = SWTransparentValue;    // set to 0
  652.     }
  653.     return ( false );
  654. }
  655.  
  656.  
  657. #pragma mark -
  658. ///--------------------------------------------------------------------------------------
  659. //    SWClearStickyError
  660. ///--------------------------------------------------------------------------------------
  661.  
  662. SW_FUNC void SWClearStickyError(void)
  663. {
  664.     gSWStickyError = 0;
  665. }
  666.  
  667. ///--------------------------------------------------------------------------------------
  668. //    SWStickyError
  669. ///--------------------------------------------------------------------------------------
  670.  
  671. SW_FUNC OSErr SWStickyError(void)
  672. {
  673.     return gSWStickyError;
  674. }
  675.  
  676. ///--------------------------------------------------------------------------------------
  677. //    SWSetStickyIfError
  678. ///--------------------------------------------------------------------------------------
  679.  
  680. SW_FUNC void SWSetStickyIfError(OSErr errNum)
  681. {
  682.     if ( errNum != noErr )
  683.         gSWStickyError = errNum;
  684. }
  685.  
  686.  
  687. ///--------------------------------------------------------------------------------------
  688. // SWHasSystem7
  689. ///--------------------------------------------------------------------------------------
  690.  
  691. SW_FUNC Boolean SWHasSystem7(void)
  692. {
  693.     long    versionNumber;
  694.     OSErr    err;
  695.     
  696.     err = Gestalt( gestaltSystemVersion, &versionNumber );
  697.  
  698.     SWSetStickyIfError( err );
  699.     return (err == noErr) && (versionNumber >= 0x0700);
  700. }
  701.  
  702.  
  703. ///--------------------------------------------------------------------------------------
  704. // SWGetProcessorType
  705. ///--------------------------------------------------------------------------------------
  706.  
  707. SW_FUNC OSErr SWGetProcessorType(short *processorType)
  708. {
  709.     long        response;
  710.     Boolean        powerMac = true;
  711.     short        processor = -1;
  712.     OSErr        err = noErr;        
  713.  
  714.     err = Gestalt(gestaltSysArchitecture,&response);
  715.     if (err == noErr)
  716.     {
  717.         switch (response)
  718.         {
  719.             case gestalt68k:            powerMac = false;        break;
  720.             case gestaltPowerPC:        powerMac = true;        break;
  721.         }
  722.     
  723.         if (!powerMac)
  724.         {
  725.             // 68k
  726.             err = Gestalt(gestaltProcessorType,&response);
  727.             if (err == noErr)
  728.             {
  729.                 switch (response)
  730.                 {
  731.                     case gestalt68000:        processor = 68000;        break;
  732.                     case gestalt68010:        processor = 68010;        break;
  733.                     case gestalt68020:        processor = 68020;        break;
  734.                     case gestalt68030:        processor = 68030;        break;
  735.                     case gestalt68040:        processor = 68040;        break;
  736.                 }
  737.             }
  738.         }
  739.         else
  740.         {
  741.             // PPC
  742.             err = Gestalt(gestaltNativeCPUtype,&response);
  743.             if (err == noErr)
  744.             {
  745.                 switch (response)
  746.                 {
  747.                     case gestaltCPU601:        processor = 601;        break;
  748.                     case gestaltCPU603:
  749.                 /*    case gestaltCPU603e:*/    processor = 603;        break;
  750.                     case gestaltCPU604:        processor = 604;        break;
  751.                 }
  752.             }
  753.         }
  754.     }
  755.  
  756.     *processorType = processor;
  757.     
  758.     return err;
  759. }
  760.  
  761.  
  762. ///--------------------------------------------------------------------------------------
  763. // SW_ABS - return the absolute value of a short
  764. ///--------------------------------------------------------------------------------------
  765.  
  766. SW_FUNC short SW_ABS(short theNum)
  767. {
  768.     if (theNum >= 0)
  769.         return theNum;
  770.     else
  771.         return -theNum;
  772. }
  773.  
  774.  
  775. #if (SW_ASSERT_ON == 1)
  776. ///--------------------------------------------------------------------------------------
  777. // SWAssertFail - called when the expression in SW_ASSERT() is false.
  778. ///--------------------------------------------------------------------------------------
  779.  
  780. void SWAssertFail(char* filename, int lineNum)
  781. {
  782.     Str255            fileString = "\p";
  783.     Str15            lineString;
  784.     short            n, result;
  785.     unsigned long    junkTime;
  786.  
  787.     NumToString((long)lineNum, lineString);
  788.     
  789.     for (n = 0; filename[n] != 0; n++)
  790.     {
  791.         fileString[n+1] = filename[n];
  792.     }
  793.     fileString[0] += n;
  794.     
  795.     SetCursor(&qd.arrow);
  796.     ShowCursor();
  797.     ParamText("\pAssertion failure! This program will now quit. The failure occurred here:", 
  798.             fileString, lineString, "\pN/A");
  799.     result = StopAlert(kAssertAlertID, NULL);
  800.     
  801.     if (result < 0)
  802.     {
  803.         SysBeep(1);
  804.         Delay(15, &junkTime); // Wait for beep to finish
  805.     }
  806.     
  807.     ExitToShell();
  808. }
  809. #endif
  810.  
  811.